package com.cc.controller

import com.cc.common.Update
import com.cc.common.model.Query
import com.cc.common.model.Results
import com.cc.model.dto.RepositoryDto
import com.cc.service.RepositoryService
import jakarta.validation.Valid
import jakarta.validation.constraints.NotBlank
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*

/**
 * 资源库管理
 *
 * @author aa
 * @since 2024/4/24
 * @version 1.0
 */
@RestController
@RequestMapping("dataCollection/repository")
class RepositoryController(private val repositoryService: RepositoryService) {


    /**
     * 添加资源库
     *
     * @param repository [repository]
     * @return [Result]
     */
    @PostMapping("/add")
    fun add(@RequestBody repository: RepositoryDto) = Results.Ok(repositoryService.add(repository))


    /**
     * 通过id删除资源库
     *
     * @param id 要删除的数据的id
     * @return [Result]
     */
    @PostMapping("/delete")
    fun delete(@RequestParam id: String) = Results.Ok(repositoryService.removeById(id))


    /**
     * 批量删除资源库
     *
     * @param ids 要删除数据的[List]集
     * @return [Result]
     */
    @PostMapping("/deleteBatch")
    fun deleteBatch(ids: List<String>) = Results.Ok(repositoryService.removeByIds(ids))


    /**
     * 更新资源库
     *
     * @param repository [repository]
     * @return [Result]
     */
    @PostMapping("/update")
    fun update(@RequestBody @Validated(Update::class) repository: RepositoryDto) = Results.Ok(repositoryService.update(repository))


    /**
     * 根据条件查询资源库列表
     *
     * @param query
     * @return
     */
    @PostMapping("/findRepListByPage")
    fun findRepListByPage(@RequestBody query: Query<RepositoryDto?>) = repositoryService.findRepListByPage(query)


    /**
     * 查询资源库明细
     *
     * @param id 根据id查询
     * @return [Result]
     */
    @GetMapping("/getRepositoryDetail")
    fun getRepositoryDetail(@Valid @NotBlank(message = "请选择查询数据") id: String) = Results.Ok(repositoryService.getRepositoryDetail(id))


    /**
     * 资源库列表
     *
     * @return [Result]
     */
    @GetMapping("/findRepList")
    fun findRepList() = Results.Ok(repositoryService.findRepList())


    /**
     * 根据资源库id查询资源库中job作业内容树
     * @param id   根据id查询
     * @param type 类型：1 job；1trans
     * @return [Result]
     */
    @GetMapping("findJobRepTreeById")
    fun findJobRepTreeById(id: String, @RequestParam(defaultValue = "", required = false) type: String) =
        Results.Ok(repositoryService.findRepTreeById(id, type))


    /**
     * 验证资源库名是否存在
     *
     * @param repId   资源库ID
     * @param repName 资源库名
     * @return 只能返回true或false
     */
    @PostMapping("repNameExist")
    fun repNameExist(
        @RequestParam(defaultValue = "", required = false) repId: String,
        @RequestParam(defaultValue = "", required = false) repName: String,
    ) = Results.Ok(repositoryService.repNameExist(repId, repName))






}